﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;

namespace ChoWorkflowDashboard
{
    public abstract class ChoBaseWindow : MetroWindow
    {
        public bool Silent { get; set; }
        protected Window ChildWindow;
        protected bool _promptSaveChanges = true;

        public ChoBaseWindow()
        {
            this.Activated += new EventHandler(ChoBaseWindow_Activated);
            Silent = false;
        }

        void ChoBaseWindow_Activated(object sender, EventArgs e)
        {
            if (ChildWindow != null)
                ChildWindow.Activate();
        }

        protected virtual void Refresh()
        {
        }

        internal void RaiseRefreshData()
        {
            if (Application.Current != null && Application.Current.Dispatcher != null)
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(Refresh));
            }
        }
    }

    public abstract class ChoBaseWindowEx : MetroWindow
    {
        public bool Silent { get; set; }
        protected Window ChildWindow;
        protected bool _promptSaveChanges = true;

        public ChoBaseWindowEx()
        {
            this.Activated += new EventHandler(ChoBaseWindow_Activated);
            this.Closing += ChoBaseWindow_Closing;
            Silent = false;
        }

        private void ChoBaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!Silent)
                e.Cancel = !PromptSaveChanges(_promptSaveChanges);

            if (ChildWindow != null)
            {
                try
                {
                    if (ChildWindow != null)
                        ChildWindow.Close();
                }
                catch
                {
                }
                finally
                {
                    ChildWindow = null;
                }
            }
        }

        private bool PromptSaveChanges(bool prompt = true)
        {
            try
            {
                if (HasChanges)
                {
                    MessageBoxResult result = prompt ? MessageBox.Show("Do you want to save changes?", Application.Current.MainWindow.Title, MessageBoxButton.YesNoCancel, MessageBoxImage.Information, MessageBoxResult.Yes) : MessageBoxResult.Yes;
                    if (result == MessageBoxResult.No)
                    {
                        RejectChanges();
                        return true;
                    }
                    else if (result == MessageBoxResult.Cancel)
                    {
                        return false;
                    }
                    else if (!UpdateChanges())
                    {
                        return false;
                    }
                }

                return true;
            }
            finally
            {
                _promptSaveChanges = true;
            }
        }

        protected abstract void RejectChanges();
        protected abstract bool UpdateChanges();

        protected abstract bool HasChanges
        {
            get;
        }

        void ChoBaseWindow_Activated(object sender, EventArgs e)
        {
            if (ChildWindow != null)
                ChildWindow.Activate();
        }

        protected virtual void Refresh()
        {
        }

        internal void RaiseRefreshData()
        {
            if (Application.Current != null && Application.Current.Dispatcher != null)
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(Refresh));
            }
        }
    }
}
